<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace app\controller\adminapi\platform\wechat;


use app\controller\adminapi\platform\PlatformController;
use app\common\controller\AdminController;
use app\common\model\wechat\WechatShare as WechatShareModel;
use hiduoke\services\WechatService;
use think\App;

/**
 * Class WechatOrder 微信订单
 * @package app\controller\adminapi\platform\wechat
 * User: liuhongbin
 * Date: 2021-06-19 18:15
 */
class WechatShare extends PlatformController
{
    /**
     * @var WechatShareModel
     */
    private $model;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new WechatShareModel();
    }

    /**
     * 显示资源列表
     * @Route("WechatUser", method="GET")
     * @return \think\Response
     */
    public function index()
    {
        $res = $this->model->getPageList(['scope' => 'plaid']);
        return $this->success($res['count'], $res['data']->append(['addons', 'wechat_order', 'exec_time_text']));
    }

    /**
     * 显示指定的资源
     * @Route("restful/id", method="GET")
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        $order = $this->model->find($id);
        return $this->success("ok", $order);
    }

    public function receivers()
    {
        $res = $this->model->Receiver->getPageList(['scope' => 'plaid']);
        return $this->success($res['count'], $res['data']);
    }

    public function showReceiver($id)
    {
        $receiver = $this->model->Receiver->find($id);
        return $this->success($id, $receiver);
    }

    //执行分账
    public function handleShare($id)
    {
        $share = $this->model->where(['id' => $id])->find();
        if ($share['status'] == "FINISHED") {
            return $this->error('当前订单已完成分账');
        }
        $receivers = $share['receivers'];
        if ($receivers && count($receivers) > 0) {
            $res = (new WechatService())->share($share['transaction_id'], $receivers, $share['out_order_no']);
            if ($res['return_code'] == "FAIL") {
                return $this->error($res['return_msg']);
            }
            if ($res['return_code'] == "SUCCESS" && $res['result_code'] == 'SUCCESS') {
                return $this->success('分账成功', $res);
            } else {
                return $this->error($res['err_code_des']);
            }
        } else {
            return $this->error('分账发放的目标用户不正确');
        }
    }

    //分账完结
    public function shareFinish($id)
    {
        $share = $this->model->where(['id' => $id])->find();
        if ($share['status'] == "FINISHED") {
            return $this->error('当前订单已完成分账');
        }
        $res = (new WechatService())->shareFinish($share['transaction_id']);
        if ($res['result_code'] == "SUCCESS" && $res['return_code'] == "SUCCESS") {
            return $this->success('分账完结成功', $res);
        } else {
            return $this->error($res['err_code_des']);
        }
    }
}
